package com.flot.yazb.home.setting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.flot.yazb.R;
import com.flot.yazb.base.BActivity;
import com.flot.yazb.base.BApplication;
import com.flot.yazb.base.BDialog;
import com.flot.yazb.base.adapter.recycleview.BRAdapter;
import com.flot.yazb.base.adapter.recycleview.RvHolder;
import com.flot.yazb.base.model.BModel;
import com.flot.yazb.aop.stub.CodeStub;
import com.flot.yazb.model.user.OrderModel;
import com.flot.yazb.model.user.UserModel;
import com.flot.yazb.widget.utils.TimeUtils;
import com.flot.yazb.widget.utils.ToastUtil;
import com.flot.yazb.widget.MRecycleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by fengdongfei on 2017/9/18.
 */

public class OrderListActivity extends BActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.mrv)
    MRecycleView mrv;
    private List<OrderModel> dataList = new ArrayList<>();
    private BRAdapter<OrderModel> rvAdapter;
    private UserModel mModel= BApplication.getInstance().getUserModel();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        ButterKnife.bind(this);
        initView();
        getData();
    }

    /**
     * 获取接口数据
     */
    private void getData() {
        OrderModel.geOrderList(mModel.id, new BModel.Transit<OrderModel[]>(this) {
            @Override
            public void onBorn(OrderModel[] serializable, String code, String message) {
                if (code.equals(CodeStub.B_OK) && serializable!=null){
                    dataList.clear();
                    dataList.addAll(Arrays.asList(serializable));
                    rvAdapter.notifyDataSetChanged();
                    if (serializable.length==0){
                        ToastUtil.showToast(activity,"暂无充值记录");
                    }
                }else{
//                    ToastUtil.showToast(activity,message);
                }
            }
        });
    }

    private void initView() {
        mrv.setLayoutManager(new LinearLayoutManager(this));
        mrv.setLoadingMoreEnabled(false);
        mrv.setPullRefreshEnabled(false);
        rvAdapter = new BRAdapter<OrderModel>(this, R.layout.activity_order_item, dataList) {
            @Override
            protected void convert(RvHolder holder, OrderModel orderModel, int position) {
                holder.setText(R.id.tvmoney,orderModel.money);
                holder.setText(R.id.tvtime, TimeUtils.formatTime(Long.valueOf(orderModel.pay_time), true, mContext));
                holder.setText(R.id.tvnum,orderModel.pay_no);
                holder.setOnClickListener(R.id.rebackmoney, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RebackDialog  bacdi= new RebackDialog(OrderListActivity.this,"确认是否真的退款！");
                        bacdi.show();
                    }
                });
            }
        };
        mrv.setAdapter(rvAdapter);
    }

    @OnClick({R.id.iv_back,R.id.kefu})
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.kefu:
                startActivity(new Intent(OrderListActivity.this,KeFuActivity.class));
                break;
        }

    }


   public class RebackDialog extends BDialog{
       private String desc;
       @BindView(R.id.tvContent)
       TextView tvContent;
       @BindView(R.id.cancle)
       TextView cancle;
       @BindView(R.id.ok)
       TextView ok;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.dialog_pay);
           ButterKnife.bind(this);
           tvContent.setText(desc);
       }

       public RebackDialog(Context context, String con) {
           super(context);
           this.desc = con;
       }

       @Override
        public boolean isCancelable(boolean b) {
            return false;
        }

        @Override
        public int setGravity(int gravity) {
            return 0;
        }

        @Override
        public int setPadWidth(float width) {
            return 0;
        }

        @Override
        public int setPadHeight(float height) {
            return 0;
        }

       @OnClick({R.id.cancle, R.id.ok})
       public void onClick(View view) {
           switch (view.getId()) {
               case R.id.cancle:
                   ToastUtil.showToast(activity,"取消退款成功");
                   dismiss();
                   break;
               case R.id.ok:
                   ToastUtil.showToast(activity,"48小时后退款到原充值账户");
                   dismiss();
                   break;
           }
       }
    };

}
